/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: ZoneManageController.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年1月15日 下午3:45:59
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.web.basedata.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpt.framework.web.JsonResult;
import com.material.c2c.basedata.entity.DLXX_XZDY;
import com.material.c2c.basedata.model.ZoneManageDTO;
import com.material.c2c.basedata.model.ZoneManageQuery;
import com.material.c2c.basedata.service.ProjZoneManageService;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.util.BeanUtils;

/**
 * 行政信息控制器
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Controller
@RequestMapping("/basedata/zoneManage")
public class ZoneManageController {

  @Autowired
  private ProjZoneManageService ProjZoneManageService;

  /**
   * 逐级取得行政地域信息
   */
  @RequestMapping(value = "/list", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult list(@RequestBody ZoneManageQuery inputs) {
    JsonResult jsonResult = null;
    try {
      // 取顶级地域信息时，父id为null，但为了便于根据父id进行缓存，所以设定将顶级地域信息的父id设置为-1。
      List<ZoneManageDTO> resultList = ProjZoneManageService
          .findSubZoneListByParrentId(null == inputs.getSjid() ? "-1" : inputs.getSjid());
      if (null != resultList && !resultList.isEmpty()) {
        for (ZoneManageDTO zManageDTO : resultList) {
          List<ZoneManageDTO> subZoneList = ProjZoneManageService.findSubZoneListByParrentId(zManageDTO.getId());
          if (null != subZoneList && !subZoneList.isEmpty()) {
            zManageDTO.setHasChildren(true);
          } else {
            zManageDTO.setHasChildren(false);
          }
        }
      }
      jsonResult = JsonResult.success(resultList);
    } catch (Exception e) {
      e.printStackTrace();
      jsonResult = JsonResult.FAILED;
    }
    return jsonResult;
  }

  /**
   * 新增行政地域信息
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult add(@RequestBody DLXX_XZDY inputs) {
    JsonResult jsonResult = null;
    try {
      if (null == inputs.getSjid()) {
        // 没有上级标识，级别就设置为7，表示是国家/地区
        inputs.setJb(7);
      } else {
        DLXX_XZDY parentZone = ProjZoneManageService.getEntityById(inputs.getSjid());
        if (null == parentZone) {
          jsonResult = JsonResult.failed("指定的父级标识不存在");
          return jsonResult;
        }
        // 父级别为1表示是组级别，那组级别下就不能再增加地域信息了
        if (parentZone.getJb() == 1) {
          jsonResult = JsonResult.failed("组级别下不能再增加地域信息");
          return jsonResult;
        }
        // 新增的地域信息的级别是父地域信息的级别减一
        inputs.setJb(parentZone.getJb() - 1);
      }
      inputs.setId(UUIDGenerator.generate().toString());
      if (ProjZoneManageService.validateZoneExistsByJbAndBm(inputs.getJb(), inputs.getBm(), inputs.getId())) {
        jsonResult = JsonResult.failed("该级别内地域编码已经存在");
        return jsonResult;
      } else {
        ProjZoneManageService.add(inputs, null == inputs.getSjid() ? "-1" : inputs.getSjid());
        jsonResult = JsonResult.SUCCEED;
      }
    } catch (Exception e) {
      e.printStackTrace();
      jsonResult = JsonResult.FAILED;
    }
    return jsonResult;
  }

  /**
   * 修改行政地域信息
   */
  @RequestMapping(value = "/update", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult update(@RequestBody DLXX_XZDY inputs) {
    JsonResult jsonResult = null;
    try {
      DLXX_XZDY sourceEntity = ProjZoneManageService.getEntityById(inputs.getId());
      if (ProjZoneManageService.validateZoneExistsByJbAndBm(sourceEntity.getJb(), inputs.getBm(), sourceEntity.getId())) {
        jsonResult = JsonResult.failed("该级别内地域编码已经存在");
      } else {
        BeanUtils.copyBeanNotNull2Bean(inputs, sourceEntity);
        ProjZoneManageService.update(sourceEntity, null == sourceEntity.getSjid() ? "-1" : sourceEntity.getSjid());
        jsonResult = JsonResult.SUCCEED;
      }
    } catch (Exception e) {
      e.printStackTrace();
      jsonResult = JsonResult.FAILED;
    }
    return jsonResult;
  }

  /**
   * 查看行政地域详情信息
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  public JsonResult detail(@PathVariable("id") String id) {
    JsonResult jsonResult = null;
    try {
      DLXX_XZDY entity = ProjZoneManageService.getEntityById(id);
      jsonResult = JsonResult.success(entity);
    } catch (Exception e) {
      e.printStackTrace();
      jsonResult = JsonResult.FAILED;
    }
    return jsonResult;
  }

  /**
   * 删除指定的行政地域及其子地域信息
   */
  @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
  @ResponseBody
  public JsonResult delete(@PathVariable("id") String id) {
    JsonResult jsonResult = null;
    try {
      DLXX_XZDY sourceEntity = ProjZoneManageService.getEntityById(id);
      List<String> ids = ProjZoneManageService.getIdStrByZoneIdAndChildrenZoneId(id);
      if (ProjZoneManageService.isExistsContracteeData(ids)) {
        jsonResult = JsonResult.failed("该行政地域下存在有发包方数据不能删除");
      } else {
        ProjZoneManageService.deleteSpecifyZoneAndSubZone(ids,
            null == sourceEntity.getSjid() ? "-1" : sourceEntity.getSjid());
        jsonResult = JsonResult.SUCCEED;
      }
    } catch (Exception e) {
      e.printStackTrace();
      jsonResult = JsonResult.FAILED;
    }
    return jsonResult;
  }
}